home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / doc / interpreter / basics.tex < prev    next >
Text File  |  1997-08-13  |  39KB  |  1,133 lines

  1. @c Copyright (C) 1996, 1997 John W. Eaton
  2. @c This is part of the Octave manual.
  3. @c For copying conditions, see the file gpl.tex.
  4.  
  5. @node Getting Started, Data Types, Introduction, Top
  6. @chapter Getting Started
  7.  
  8. This chapter explains some of Octave's basic features, including how to
  9. start an Octave session, get help at the command prompt, edit the
  10. command line, and write Octave programs that can be executed as commands
  11. from your shell.
  12.  
  13. @menu
  14. * Invoking Octave::             
  15. * Quitting Octave::             
  16. * Getting Help::                
  17. * Command Line Editing::        
  18. * Errors::                      
  19. * Executable Octave Programs::  
  20. * Comments::                    
  21. @end menu
  22.  
  23. @node Invoking Octave, Quitting Octave, Getting Started, Getting Started
  24. @section Invoking Octave
  25.  
  26. Normally, Octave is used interactively by running the program
  27. @samp{octave} without any arguments.  Once started, Octave reads
  28. commands from the terminal until you tell it to exit.
  29.  
  30. You can also specify the name of a file on the command line, and Octave
  31. will read and execute the commands from the named file and then exit
  32. when it is finished.
  33.  
  34. You can further control how Octave starts by using the command-line
  35. options described in the next section, and Octave itself can remind you
  36. of the options available.  Type @samp{octave --help} to display all
  37. available options and briefly describe their use (@samp{octave -h} is a
  38. shorter equivalent).
  39.  
  40. @menu
  41. * Command Line Options::        
  42. * Startup Files::               
  43. @end menu
  44.  
  45. @node Command Line Options, Startup Files, Invoking Octave, Invoking Octave
  46. @subsection Command Line Options
  47. @cindex Octave command options
  48. @cindex command options
  49. @cindex options, Octave command
  50.  
  51. Here is a complete list of all the command line options that Octave
  52. accepts.
  53.  
  54. @table @code
  55. @item --debug
  56. @itemx -d
  57. @cindex @code{--debug}
  58. @cindex @code{-d}
  59. Enter parser debugging mode.  Using this option will cause Octave's
  60. parser to print a lot of information about the commands it reads, and is
  61. probably only useful if you are actually trying to debug the parser.
  62.  
  63. @item --echo-commands
  64. @itemx -x
  65. @cindex @code{--echo-commands}
  66. @cindex @code{-x}
  67. Echo commands as they are executed.
  68.  
  69. @item --exec-path @var{path}
  70. @cindex @code{--exec-path @var{path}}
  71. Specify the path to search for programs to run.  The value of @var{path}
  72. specified on the command line will override any value of
  73. @code{OCTAVE_EXEC_PATH} found in the environment, but not any commands
  74. in the system or user startup files that set the built-in variable
  75. @code{EXEC_PATH}.
  76.  
  77. @item --help
  78. @itemx -h
  79. @itemx -?
  80. @cindex @code{--help}
  81. @cindex @code{-h}
  82. @cindex @code{-?}
  83. Print short help message and exit.
  84.  
  85. @item --info-file @var{filename}
  86. @cindex @code{--info-file @var{filename}}
  87. Specify the name of the info file to use.  The value of @var{filename}
  88. specified on the command line will override any value of
  89. @code{OCTAVE_INFO_FILE} found in the environment, but not any commands
  90. in the system or user startup files that set the built-in variable
  91. @code{INFO_FILE}.
  92.  
  93. @item --info-program @var{program}
  94. @cindex @code{--info-program @var{program}}
  95. Specify the name of the info program to use.  The value of @var{program}
  96. specified on the command line will override any value of
  97. @code{OCTAVE_INFO_PROGRAM} found in the environment, but not any
  98. commands in the system or user startup files that set the built-in
  99. variable @code{INFO_PROGRAM}.
  100.  
  101. @item --interactive
  102. @itemx -i
  103. @cindex @code{--interactive}
  104. @cindex @code{-i}
  105. Force interactive behavior.  This can be useful for running Octave via a
  106. remote shell command or inside an Emacs shell buffer.  For another way
  107. to run Octave within Emacs, see @ref{Emacs}.
  108.  
  109. @item --no-init-file
  110. @cindex @code{--no-init-file}
  111. Don't read the @file{~/.octaverc} or @file{.octaverc} files.
  112.  
  113. @item --no-line-editing
  114. @cindex @code{--no-line-editing}
  115. Disable command-line editing and history.
  116.  
  117. @item --no-site-file
  118. @cindex @code{--no-site-file}
  119. Don't read the site-wide @file{octaverc} file.
  120.  
  121. @item --norc
  122. @itemx -f
  123. @cindex @code{--norc}
  124. @cindex @code{-f}
  125. Don't read any of the system or user initialization files at startup.
  126. This is equivalent to using both of the options @code{--no-init-file}
  127. and @code{--no-site-file}.
  128.  
  129. @item --path @var{path}
  130. @itemx -p @var{path}
  131. @cindex @code{--path @var{path}}
  132. @cindex @code{-p @var{path}}
  133. Specify the path to search for function files.  The value of @var{path}
  134. specified on the command line will override any value of
  135. @code{OCTAVE_PATH} found in the environment, but not any commands in the
  136. system or user startup files that set the built-in variable @code{LOADPATH}.
  137.  
  138. @item --silent
  139. @itemx --quiet
  140. @itemx -q
  141. @cindex @code{--silent}
  142. @cindex @code{--quiet}
  143. @cindex @code{-q}
  144. Don't print the usual greeting and version message at startup.
  145.  
  146. @item --traditional
  147. @itemx --braindead
  148. @cindex @code{--traditional}
  149. @cindex @code{--braindead}
  150. Set initial values for user-preference variables to the following
  151. values for compatibility with @sc{Matlab}.
  152.  
  153. @example
  154. PS1                           = ">> "
  155. PS2                           = ""
  156. beep_on_error                 = 1
  157. default_save_format           = "mat-binary"
  158. define_all_return_values      = 1
  159. do_fortran_indexing           = 1
  160. empty_list_elements_ok        = 1
  161. implicit_str_to_num_ok        = 1
  162. ok_to_lose_imaginary_part     = 1
  163. page_screen_output            = 0
  164. prefer_column_vectors         = 0
  165. print_empty_dimensions        = 0
  166. treat_neg_dim_as_zero         = 1
  167. warn_function_name_clash      = 0
  168. whitespace_in_literal_matrix  = "traditional"
  169. @end example
  170.  
  171. @item --verbose
  172. @itemx -V
  173. @cindex @code{--verbose}
  174. @cindex @code{-V}
  175. Turn on verbose output.
  176.  
  177. @item --version
  178. @itemx -v
  179. @cindex @code{--version}
  180. @cindex @code{-v}
  181. Print the program version number and exit.
  182.  
  183. @item @var{file}
  184. Execute commands from @var{file}.
  185. @end table
  186.  
  187. Octave also includes several built-in variables that contain information
  188. about the command line, including the number of arguments and all of the
  189. options.
  190.  
  191. @defvr {Built-in Variable} argv
  192. The command line arguments passed to Octave are available in this
  193. variable.  For example, if you invoked Octave using the command
  194.  
  195. @example
  196. octave --no-line-editing --silent
  197. @end example
  198.  
  199. @noindent
  200. @code{argv} would be a string vector with the elements
  201. @code{--no-line-editing} and @code{--silent}.
  202.  
  203. If you write an executable Octave script, @code{argv} will contain the
  204. list of arguments passed to the script.  @pxref{Executable Octave Programs}.
  205. @end defvr
  206.  
  207. @defvr {Built-in Variable} nargin
  208. At the top level, this variable is defined as the number of command line
  209. arguments that were passed to Octave.
  210. @end defvr
  211.  
  212. @defvr {Built-in Variable} program_invocation_name
  213. @defvrx {Built-in Variable} program_name
  214. When Octave starts, the value of the built-in variable
  215. @code{program_invocation_name} is automatically set to the name that was
  216. typed at the shell prompt to run Octave, and the value of
  217. @code{program_name} is automatically set to the final component of
  218. @code{program_invocation_name}.  For example, if you typed
  219. @samp{@value{OCTAVEHOME}/bin/octave} to start Octave,
  220. @code{program_invocation_name} would have the value
  221. @code{"@value{OCTAVEHOME}/bin/octave"}, and @code{program_name} would
  222. have the value @code{"octave"}.
  223.  
  224. If executing a script from the command line (e.g., @code{octave foo.m}
  225. or using an executable Octave script, the program name is set to the
  226. name of the script.  @xref{Executable Octave Programs} for an example of
  227. how to create an executable Octave script.
  228. @end defvr
  229.  
  230. Here is an example of using these variables to reproduce Octave's
  231. command line.
  232.  
  233. @example
  234. printf ("%s", program_name);
  235. for i = 1:nargin
  236.   printf (" %s", argv(i,:));
  237. endfor
  238. printf ("\n");
  239. @end example
  240.  
  241. @noindent
  242. @xref{Index Expressions} for an explanation of how to properly index
  243. arrays of strings and substrings in Octave.
  244.  
  245. @node Startup Files,  , Command Line Options, Invoking Octave
  246. @subsection Startup Files
  247. @cindex initialization
  248. @cindex startup
  249.  
  250. When Octave starts, it looks for commands to execute from the following
  251. files:
  252.  
  253. @cindex startup files
  254.  
  255. @table @code
  256. @item @var{octave-home}/share/octave/site/m/startup/octaverc
  257. Where @var{octave-home} is the directory in which all of Octave is
  258. installed (the default is @file{@value{OCTAVEHOME}}).  This file is
  259. provided so that changes to the default Octave environment can be made
  260. globally for all users at your site for all versions of Octave you have
  261. installed.  Some care should be taken when making changes to this file,
  262. since all users of Octave at your site will be affected.
  263.  
  264. @item @var{octave-home}/share/octave/@var{version}/m/startup/octaverc
  265. Where @var{octave-home} is the directory in which all of Octave is
  266. installed (the default is @file{@value{OCTAVEHOME}}), and @var{version}
  267. is the version number of Octave.  This file is provided so that changes
  268. to the default Octave environment can be made globally for all users for
  269. a particular version of Octave.  Some care should be taken when making
  270. changes to this file, since all users of Octave at your site will be
  271. affected.
  272.  
  273. @item ~/.octaverc
  274. @cindex @code{~/.octaverc}
  275. This file is normally used to make personal changes to the default
  276. Octave environment.
  277.  
  278. @item .octaverc
  279. @cindex @code{.octaverc}
  280. This file can be used to make changes to the default Octave environment
  281. for a particular project.  Octave searches for this file in the current
  282. directory after it reads @file{~/.octaverc}.  Any use of the @code{cd}
  283. command in the @file{~/.octaverc} file will affect the directory that
  284. Octave searches for the file @file{.octaverc}.
  285.  
  286. If you start Octave in your home directory, commands from from the file
  287. @file{~/.octaverc} will only be executed once.
  288. @end table
  289.  
  290. A message will be displayed as each of the startup files is read if you
  291. invoke Octave with the @code{--verbose} option but without the
  292. @code{--silent} option.
  293.  
  294. Startup files may contain any valid Octave commands, including function
  295. definitions.
  296.  
  297. @node Quitting Octave, Getting Help, Invoking Octave, Getting Started
  298. @section Quitting Octave
  299. @cindex exiting octave
  300. @cindex quitting octave
  301.  
  302. @deftypefn {Built-in Function} {} exit (@var{status})
  303. @deftypefnx {Built-in Function} {} quit (@var{status})
  304. Exit the current Octave session.  If the optional integer value
  305. @var{status} is supplied, pass that value to the operating system as the
  306. Octave's exit status.
  307. @end deftypefn
  308.  
  309. @deftypefn {Built-in Function} {} atexit (@var{fcn})
  310. Register function to be called when Octave exits.  For example,
  311.  
  312. @example
  313. @group
  314. function print_flops_at_exit ()
  315.   printf ("\n%s\n", system ("fortune"));
  316.   fflush (stdout);
  317. endfunction
  318. atexit ("print_flops_at_exit");
  319. @end group
  320. @end example
  321.  
  322. @noindent
  323. will print a message when Octave exits.
  324. @end deftypefn
  325.  
  326. @node Getting Help, Command Line Editing, Quitting Octave, Getting Started
  327. @section Commands for Getting Help
  328. @cindex on-line help
  329. @cindex help, on-line
  330.  
  331. The entire text of this manual is available from the Octave prompt
  332. via the command @kbd{help -i}.  In addition, the documentation for
  333. individual user-written functions and variables is also available via
  334. the @kbd{help} command.  This section describes the commands used for
  335. reading the manual and the documentation strings for user-supplied
  336. functions and variables.  @xref{Function Files}, for more information
  337. about how to document the functions you write.
  338.  
  339. @deffn {Command} help
  340. Octave's @code{help} command can be used to print brief usage-style
  341. messages, or to display information directly from an on-line version of
  342. the printed manual, using the GNU Info browser.  If invoked without any
  343. arguments, @code{help} prints a list of all the available operators,
  344. functions, and built-in variables.  If the first argument is @code{-i},
  345. the @code{help} command searches the index of the on-line version of
  346. this manual for the given topics.
  347.  
  348. For example, the command @kbd{help help} prints a short message
  349. describing the @code{help} command, and @kbd{help -i help} starts the
  350. GNU Info browser at this node in the on-line version of the manual.
  351.  
  352. Once the GNU Info browser is running, help for using it is available
  353. using the command @kbd{C-h}.
  354. @end deffn
  355.  
  356. The help command can give you information about operators, but not the
  357. comma and semicolons that are used as command separators.  To get help
  358. for those, you must type @kbd{help comma} or @kbd{help semicolon}.
  359.  
  360. @defvr {Built-in Variable} INFO_FILE
  361. The variable @code{INFO_FILE} names the location of the Octave info file.
  362. The default value is @code{"@var{octave-home}/info/octave.info"}, where
  363. @var{octave-home} is the directory where all of Octave is installed.
  364. @end defvr
  365.  
  366. @defvr {Built-in Variable} INFO_PROGRAM
  367. The variable @code{INFO_PROGRAM} names the info program to run.  Its
  368. initial value is
  369. @code{"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info"},
  370. where @var{octave-home} is the directory where all of Octave is
  371. installed, @var{version} is the Octave version number, and @var{arch} is
  372. the machine type.  The value of @code{INFO_PROGRAM} can be overridden by
  373. the environment variable @code{OCTAVE_INFO_PROGRAM}, or the command line
  374. argument @code{--info-program NAME}, or by setting the value of the
  375. built-in variable @code{INFO_PROGRAM} in a startup script.
  376. @end defvr
  377.  
  378. @defvr {Built-in Variable} suppress_verbose_help_message
  379. If the value of @code{suppress_verbose_help_message} is nonzero, Octave
  380. will not add additional help information to the end of the output from
  381. the @code{help} command and usage messages for built-in commands.
  382. @end defvr
  383.  
  384. @node Command Line Editing, Errors, Getting Help, Getting Started
  385. @section Command Line Editing
  386. @cindex command-line editing
  387. @cindex editing the command line
  388.  
  389. Octave uses the GNU readline library to provide an extensive set of
  390. command-line editing and history features.  Only the most common
  391. features are described in this manual.  Please see The GNU Readline
  392. Library manual for more information.
  393.  
  394. To insert printing characters (letters, digits, symbols, etc.), simply
  395. type the character.  Octave will insert the character at the cursor and
  396. advance the cursor forward.
  397.  
  398. Many of the command-line editing functions operate using control
  399. characters.  For example, the character @kbd{Control-a} moves the cursor
  400. to the beginning of the line.  To type @kbd{C-a}, hold down @key{CTRL}
  401. and then press @key{a}.  In the following sections, control characters
  402. such as @kbd{Control-a} are written as @kbd{C-a}.
  403.  
  404. Another set of command-line editing functions use Meta characters.  On
  405. some terminals, you type @kbd{M-u} by holding down @key{META} and
  406. pressing @key{u}.  If your terminal does not have a @key{META} key, you
  407. can still type Meta charcters using two-character sequences starting
  408. with @kbd{ESC}.  Thus, to enter @kbd{M-u}, you could type
  409. @key{ESC}@key{u}.  The @kbd{ESC} character sequences are also allowed on
  410. terminals with real Meta keys.  In the following sections, Meta
  411. characters such as @kbd{Meta-u} are written as @kbd{M-u}.
  412.  
  413. @menu
  414. * Cursor Motion::               
  415. * Killing and Yanking::         
  416. * Commands For Text::           
  417. * Commands For Completion::     
  418. * Commands For History::        
  419. * Customizing the Prompt::      
  420. * Diary and Echo Commands::     
  421. @end menu
  422.  
  423. @node Cursor Motion, Killing and Yanking, Command Line Editing, Command Line Editing
  424. @subsection Cursor Motion
  425.  
  426. The following commands allow you to position the cursor.
  427.  
  428. @table @kbd
  429. @item C-b
  430. Move back one character.
  431.  
  432. @item C-f
  433. Move forward one character.
  434.  
  435. @item @key{DEL}
  436. Delete the character to the left of the cursor.
  437.  
  438. @item C-d
  439. Delete the character underneath the cursor.
  440.  
  441. @item M-f
  442. Move forward a word.
  443.  
  444. @item M-b
  445. Move backward a word.
  446.  
  447. @item C-a
  448. Move to the start of the line.
  449.  
  450. @item C-e
  451. Move to the end of the line.
  452.  
  453. @item C-l
  454. Clear the screen, reprinting the current line at the top.
  455.  
  456. @item C-_
  457. @itemx C-/
  458. Undo the last thing that you did.  You can undo all the way back to an
  459. empty line.
  460.  
  461. @item M-r
  462. Undo all changes made to this line.  This is like typing the `undo'
  463. command enough times to get back to the beginning.
  464. @end table
  465.  
  466. The above table describes the most basic possible keystrokes that you need
  467. in order to do editing of the input line.  On most terminals, you can
  468. also use the arrow keys in place of @kbd{C-f} and @kbd{C-b} to move
  469. forward and backward.
  470.  
  471. Notice how @kbd{C-f} moves forward a character, while @kbd{M-f} moves
  472. forward a word.  It is a loose convention that control keystrokes
  473. operate on characters while meta keystrokes operate on words.
  474.  
  475. There is also a function available so that you can clear the screen from
  476. within Octave programs.
  477.  
  478. @cindex clearing the screen
  479.  
  480. @deftypefn {Built-in Function} {} clc ()
  481. @deftypefnx {Built-in Function} {} home ()
  482. Clear the terminal screen and move the cursor to the upper left corner.
  483. @end deftypefn
  484.  
  485. @node Killing and Yanking, Commands For Text, Cursor Motion, Command Line Editing
  486. @subsection Killing and Yanking
  487.  
  488. @dfn{Killing} text means to delete the text from the line, but to save
  489. it away for later use, usually by @dfn{yanking} it back into the line.
  490. If the description for a command says that it `kills' text, then you can
  491. be sure that you can get the text back in a different (or the same)
  492. place later.
  493.  
  494. Here is the list of commands for killing text.
  495.  
  496. @table @kbd
  497. @item C-k
  498. Kill the text from the current cursor position to the end of the line.
  499.  
  500. @item M-d
  501. Kill from the cursor to the end of the current word, or if between
  502. words, to the end of the next word.
  503.  
  504. @item M-@key{DEL}
  505. Kill from the cursor to the start of the previous word, or if between
  506. words, to the start of the previous word. 
  507.  
  508. @item C-w
  509. Kill from the cursor to the previous whitespace.  This is different than
  510. @kbd{M-@key{DEL}} because the word boundaries differ.
  511. @end table
  512.  
  513. And, here is how to @dfn{yank} the text back into the line.  Yanking
  514. means to copy the most-recently-killed text from the kill buffer.
  515.  
  516. @table @kbd
  517. @item C-y
  518. Yank the most recently killed text back into the buffer at the cursor.
  519.  
  520. @item M-y
  521. Rotate the kill-ring, and yank the new top.  You can only do this if
  522. the prior command is @kbd{C-y} or @kbd{M-y}.
  523. @end table
  524.  
  525. When you use a kill command, the text is saved in a @dfn{kill-ring}.
  526. Any number of consecutive kills save all of the killed text together, so
  527. that when you yank it back, you get it in one clean sweep.  The kill
  528. ring is not line specific; the text that you killed on a previously
  529. typed line is available to be yanked back later, when you are typing
  530. another line.
  531.  
  532. @node Commands For Text, Commands For Completion, Killing and Yanking, Command Line Editing
  533. @subsection Commands For Changing Text
  534.  
  535. The following commands can be used for entering characters that would
  536. otherwise have a special meaning (e.g., @kbd{TAB}, @kbd{C-q}, etc.), or
  537. for quickly correcting typing mistakes.
  538.  
  539. @table @kbd
  540. @item C-q
  541. @itemx C-v
  542. Add the next character that you type to the line verbatim.  This is
  543. how to insert things like @kbd{C-q} for example.
  544.  
  545. @item M-@key{TAB}
  546. Insert a tab character.
  547.  
  548. @item C-t
  549. Drag the character before the cursor forward over the character at the
  550. cursor, also moving the cursor forward.  If the cursor is at the end of
  551. the line, then transpose the two characters before it.
  552.  
  553. @item M-t
  554. Drag the word behind the cursor past the word in front of the cursor
  555. moving the cursor over that word as well.
  556.  
  557. @item M-u
  558. Uppercase the characters following the cursor to the end of the current
  559. (or following) word, moving the cursor to the end of the word.
  560.  
  561. @item M-l
  562. Lowecase the characters following the cursor to the end of the current
  563. (or following) word, moving the cursor to the end of the word.
  564.  
  565. @item M-c
  566. Uppercase the character following the cursor (or the beginning of the
  567. next word if the cursor is between words), moving the cursor to the end
  568. of the word.
  569. @end table
  570.  
  571. @node Commands For Completion, Commands For History, Commands For Text, Command Line Editing
  572. @subsection Letting Readline Type For You
  573. @cindex command completion
  574.  
  575. The following commands allow Octave to complete command and variable
  576. names for you.
  577.  
  578. @table @kbd
  579. @item @key{TAB}
  580. Attempt to do completion on the text before the cursor.  Octave can
  581. complete the names of commands and variables.
  582.  
  583. @item M-?
  584. List the possible completions of the text before the cursor.
  585. @end table
  586.  
  587. @defvr {Built-in Variable} completion_append_char
  588. The value of @code{completion_append_char} is used as the character to
  589. append to successful command-line completion attempts.  The default
  590. value is @code{" "} (a single space).
  591. @end defvr
  592.  
  593. @deftypefn {Built-in Function} {} completion_matches (@var{hint})
  594. Generate possible completions given @var{hint}.
  595.  
  596. This function is provided for the benefit of programs like Emacs which
  597. might be controlling Octave and handling user input.  The current
  598. command number is not incremented when this function is called.  This is
  599. a feature, not a bug.
  600. @end deftypefn
  601.  
  602. @node Commands For History, Customizing the Prompt, Commands For Completion, Command Line Editing
  603. @subsection Commands For Manipulating The History
  604. @cindex command history
  605. @cindex input history
  606. @cindex history of commands
  607.  
  608. Octave normally keeps track of the commands you type so that you can
  609. recall previous commands to edit or execute them again.  When you exit
  610. Octave, the most recent commands you have typed, up to the number
  611. specified by the variable @code{history_size}, are saved in a file.
  612. When Octave starts, it loads an initial list of commands from the file
  613. named by the variable @code{history_file}.
  614.  
  615. Here are the commands for simple browsing and searching the history
  616. list.
  617.  
  618. @table @kbd
  619. @item @key{LFD}
  620. @itemx @key{RET}
  621. Accept the line regardless of where the cursor is.  If this line is
  622. non-empty, add it to the history list.  If this line was a history
  623. line, then restore the history line to its original state.
  624.  
  625. @item C-p
  626. Move `up' through the history list.
  627.  
  628. @item C-n
  629. Move `down' through the history list.
  630.  
  631. @item M-<
  632. Move to the first line in the history.
  633.  
  634. @item M->
  635. Move to the end of the input history, i.e., the line you are entering!
  636.  
  637. @item C-r
  638. Search backward starting at the current line and moving `up' through
  639. the history as necessary.  This is an incremental search.
  640.  
  641. @item C-s
  642. Search forward starting at the current line and moving `down' through
  643. the the history as necessary.
  644. @end table
  645.  
  646. On most terminals, you can also use the arrow keys in place of @kbd{C-p}
  647. and @kbd{C-n} to move through the history list.
  648.  
  649. In addition to the keyboard commands for moving through the history
  650. list, Octave provides three functions for viewing, editing, and
  651. re-running chunks of commands from the history list.
  652.  
  653. @deffn {Command} history options
  654. If invoked with no arguments, @code{history} displays a list of commands
  655. that you have executed.  Valid options are:
  656.  
  657. @table @code
  658. @item -w @var{file}
  659. Write the current history to the file @var{file}.  If the name is
  660. omitted, use the default history file (normally @file{~/.octave_hist}).
  661.  
  662. @item -r @var{file}
  663. Read the file @var{file}, replacing the current history list with its
  664. contents.  If the name is omitted, use the default history file
  665. (normally @file{~/.octave_hist}).
  666.  
  667. @item @var{N}
  668. Only display the most recent @var{N} lines of history.
  669.  
  670. @item -q
  671. Don't number the displayed lines of history.  This is useful for cutting
  672. and pasting commands if you are using the X Window System.
  673. @end table
  674.  
  675. For example, to display the five most recent commands that you have
  676. typed without displaying line numbers, use the command
  677. @kbd{history -q 5}.
  678. @end deffn
  679.  
  680. @deffn {Command} edit_history options
  681. If invoked with no arguments, @code{edit_history} allows you to edit the
  682. history list using the editor named by the variable @code{EDITOR}.  The
  683. commands to be edited are first copied to a temporary file.  When you
  684. exit the editor, Octave executes the commands that remain in the file.
  685. It is often more convenient to use @code{edit_history} to define functions 
  686. rather than attempting to enter them directly on the command line.
  687. By default, the block of commands is executed as soon as you exit the
  688. editor.  To avoid executing any commands, simply delete all the lines
  689. from the buffer before exiting the editor.
  690.  
  691. The @code{edit_history} command takes two optional arguments specifying
  692. the history numbers of first and last commands to edit.  For example,
  693. the command
  694.  
  695. @example
  696. edit_history 13
  697. @end example
  698.  
  699. @noindent
  700. extracts all the commands from the 13th through the last in the history
  701. list.  The command
  702.  
  703. @example
  704. edit_history 13 169
  705. @end example
  706.  
  707. @noindent
  708. only extracts commands 13 through 169.  Specifying a larger number for
  709. the first command than the last command reverses the list of commands
  710. before placing them in the buffer to be edited.  If both arguments are
  711. omitted, the previous command in the history list is used.
  712. @end deffn
  713.  
  714. @deffn {Command} run_history
  715. Similar to @code{edit_history}, except that the editor is not invoked,
  716. and the commands are simply executed as they appear in the history list.
  717. @end deffn
  718.  
  719. @defvr {Built-in Variable} EDITOR
  720. A string naming the editor to use with the @code{edit_history} command.
  721. If the environment variable @code{EDITOR} is set when Octave starts, its
  722. value is used as the default.  Otherwise, @code{EDITOR} is set to
  723. @code{"emacs"}.
  724. @end defvr
  725.  
  726. @defvr {Built-in Variable} history_file
  727. This variable specifies the name of the file used to store command
  728. history.  The default value is @code{"~/.octave_hist"}, but may be
  729. overridden by the environment variable @code{OCTAVE_HISTFILE}.
  730. @end defvr
  731.  
  732. @defvr {Built-in Variable} history_size
  733. This variable specifies how many entries to store in the history file.
  734. The default value is @code{1024}, but may be overridden by the
  735. environment variable @code{OCTAVE_HISTSIZE}.
  736. @end defvr
  737.  
  738. @defvr {Built-in Variable} saving_history
  739. If the value of @code{saving_history} is @code{"true"}, command entered
  740. on the command line are saved in the file specified by the variable
  741. @code{history_file}.
  742. @end defvr
  743.  
  744. @node Customizing the Prompt, Diary and Echo Commands, Commands For History, Command Line Editing
  745. @subsection Customizing the Prompt
  746. @cindex prompt customization
  747. @cindex customizing the prompt
  748.  
  749. The following variables are available for customizing the appearance of
  750. the command-line prompts.  Octave allows the prompt to be customized by
  751. inserting a number of backslash-escaped special characters that are
  752. decoded as follows:
  753.  
  754. @table @samp
  755. @item \t
  756. The time.
  757.  
  758. @item \d
  759. The date.
  760.  
  761. @item \n
  762. Begins a new line by printing the equivalent of a carriage return
  763. followed by a line feed.
  764.  
  765. @item \s
  766. The name of the program (usually just @samp{octave}).
  767.  
  768. @item \w
  769. The current working directory.
  770.  
  771. @item \W
  772. The basename of the current working directory.
  773.  
  774. @item \u
  775. The username of the current user.
  776.  
  777. @item \h
  778. The hostname, up to the first `.'.
  779.  
  780. @item \H
  781. The hostname.
  782.  
  783. @item \#
  784. The command number of this command, counting from when Octave starts.
  785.  
  786. @item \!
  787. The history number of this command.  This differs from @samp{\#} by the
  788. number of commands in the history list when Octave starts.
  789.  
  790. @item \$
  791. If the effective UID is 0, a @samp{#}, otherwise a @samp{$}.
  792.  
  793. @item \nnn
  794. The character whose character code in octal is @var{nnn}.
  795.  
  796. @item \\
  797. A backslash.
  798. @end table
  799.  
  800. @defvr {Built-in Variable} PS1
  801. The primary prompt string.  When executing interactively, Octave
  802. displays the primary prompt @code{PS1} when it is ready to read a
  803. command.
  804.  
  805. The default value of @code{PS1} is @code{"\s:\#> "}.  To change it, use a
  806. command like
  807.  
  808. @example
  809. octave:13> PS1 = "\\u@@\\H> "
  810. @end example
  811.  
  812. @noindent
  813. which will result in the prompt @samp{boris@@kremvax> } for the user
  814. @samp{boris} logged in on the host @samp{kremvax.kgb.su}.  Note that two
  815. backslashes are required to enter a backslash into a string.
  816. @xref{Strings}.
  817. @end defvr
  818.  
  819. @defvr {Built-in Variable} PS2
  820. The secondary prompt string, which is printed when Octave is
  821. expecting additional input to complete a command.  For example, when
  822. defining a function over several lines, Octave will print the value of
  823. @code{PS1} at the beginning of each line after the first.  The default
  824. value of @code{PS2} is @code{"> "}.
  825. @end defvr
  826.  
  827. @defvr {Built-in Variable} PS4
  828. If Octave is invoked with the @code{--echo-input} option, the value of
  829. @code{PS4} is printed before each line of input that is echoed.  The
  830. default value of @code{PS4} is @code{"+ "}.  @xref{Invoking Octave}, for
  831. a description of @code{--echo-input}.
  832. @end defvr
  833.  
  834. @node Diary and Echo Commands,  , Customizing the Prompt, Command Line Editing
  835. @subsection Diary and Echo Commands
  836. @cindex diary of commands and output
  837. @cindex command and ouput logs
  838. @cindex logging commands and output
  839. @cindex echoing executing commands
  840. @cindex command echoing
  841.  
  842. Octave's diary feature allows you to keep a log of all or part of an
  843. interactive session by recording the input you type and the output that
  844. Octave produces in a separate file.
  845.  
  846. @deffn {Command} diary options
  847. Create a list of all commands @emph{and} the output they produce, mixed
  848. together just as you see them on your terminal.  Valid options are:
  849.  
  850. @table @code
  851. @item on
  852. Start recording your session in a file called @file{diary} in your
  853. current working directory.
  854.  
  855. @item off
  856. Stop recording your session in the diary file.
  857.  
  858. @item @var{file}
  859. Record your session in the file named @var{file}.
  860. @end table
  861.  
  862. Without any arguments, @code{diary} toggles the current diary state.
  863. @end deffn
  864.  
  865. Sometimes it is useful to see the commands in a function or script as
  866. they are being evaluated.  This can be especially helpful for debugging
  867. some kinds of problems.
  868.  
  869. @deffn {Command} echo options
  870. Control whether commands are displayed as they are executed.  Valid
  871. options are:
  872.  
  873. @table @code
  874. @item on
  875. Enable echoing of commands as they are executed in script files.
  876.  
  877. @item off
  878. Disable echoing of commands as they are executed in script files.
  879.  
  880. @item on all
  881. Enable echoing of commands as they are executed in script files and
  882. functions.
  883.  
  884. @item off all
  885. Disable echoing of commands as they are executed in script files and
  886. functions.
  887. @end table
  888.  
  889. @noindent
  890. If invoked without any arguments, @code{echo} toggles the current echo
  891. state.
  892. @end deffn
  893.  
  894. @defvr {Built-in Variable} echo_executing_commands
  895. This variable is may also be used to control the echo state.  It may be
  896. the sum of the following values:
  897.  
  898. @table @asis
  899. @item 1
  900. Echo commands read from script files.
  901.  
  902. @item 2
  903. Echo commands from functions.
  904.  
  905. @item 4
  906. Echo commands read from command line.
  907. @end table
  908.  
  909. More than one state can be active at once.  For example, a value of 3 is
  910. equivalent to the command @kbd{echo on all}.
  911.  
  912. The value of @code{echo_executing_commands} is set by the @kbd{echo}
  913. command and the command line option @code{--echo-input}.
  914. @end defvr
  915.  
  916. @node Errors, Executable Octave Programs, Command Line Editing, Getting Started
  917. @section How Octave Reports Errors
  918. @cindex error messages
  919. @cindex messages, error
  920.  
  921. Octave reports two kinds of errors for invalid programs.
  922.  
  923. A @dfn{parse error} occurs if Octave cannot understand something you
  924. have typed.  For example, if you misspell a keyword,
  925.  
  926. @example
  927. octave:13> functon y = f (x) y = x^2; endfunction
  928. @end example
  929.  
  930. @noindent
  931. Octave will respond immediately with a message like this:
  932.  
  933. @example
  934. parse error:
  935.  
  936.   functon y = f (x) y = x^2; endfunction
  937.           ^
  938. @end example
  939.  
  940. @noindent
  941. For most parse errors, Octave uses a caret (@samp{^}) to mark the point
  942. on the line where it was unable to make sense of your input.  In this
  943. case, Octave generated an error message because the keyword
  944. @code{function} was misspelled.  Instead of seeing @samp{function f},
  945. Octave saw two consecutive variable names, which is invalid in this
  946. context.  It marked the error at the @code{y} because the first name by
  947. itself was accepted as valid input.
  948.  
  949. Another class of error message occurs occurs at evaluation time.  These
  950. errors are called @dfn{run-time errors}, or sometimes
  951. @dfn{evaluation errors} because they occur when your program is being
  952. @dfn{run}, or @dfn{evaluated}.  For example, if after correcting the
  953. mistake in the previous function definition, you type
  954.  
  955. @example
  956. octave:13> f ()
  957. @end example
  958.  
  959. @noindent
  960. Octave will respond with
  961.  
  962. @example
  963. @group
  964. error: `x' undefined near line 1 column 24
  965. error: evaluating expression near line 1, column 24
  966. error: evaluating assignment expression near line 1, column 22
  967. error: called from `f'
  968. @end group
  969. @end example
  970.  
  971. This error message has several parts, and gives you quite a bit of
  972. information to help you locate the source of the error.  The messages
  973. are generated from the point of the innermost error, and provide a
  974. traceback of enclosing expressions and function calls.
  975.  
  976. In the example above, the first line indicates that a variable named
  977. @samp{x} was found to be undefined near line 1 and column 24 of some
  978. function or expression.  For errors occurring within functions, lines
  979. from the beginning of the file containing the function definition.  For
  980. errors occurring at the top level, the line number indicates the input
  981. line number, which is usually displayed in the prompt string.
  982.  
  983. The second and third lines in the example indicate that the error
  984. occurred within an assignment expression, and the last line of the error
  985. message indicates that the error occurred within the function @code{f}.
  986. If the function @code{f} had been called from another function, for
  987. example, @code{g}, the list of errors would have ended with one more
  988. line:
  989.  
  990. @example
  991. error: called from `g'
  992. @end example
  993.  
  994. These lists of function calls usually make it fairly easy to trace the
  995. path your program took before the error occurred, and to correct the
  996. error before trying again.
  997.  
  998. @node Executable Octave Programs, Comments, Errors, Getting Started
  999. @section Executable Octave Programs
  1000. @cindex executable scripts
  1001. @cindex scripts
  1002. @cindex self contained programs
  1003. @cindex program, self contained
  1004. @cindex @samp{#!}
  1005.  
  1006. Once you have learned Octave, you may want to write self-contained
  1007. Octave scripts, using the @samp{#!} script mechanism.  You can do this
  1008. on GNU systems and on many Unix systems @footnote{The @samp{#!}
  1009. mechanism works on Unix systems derived from Berkeley Unix, System V
  1010. Release 4, and some System V Release 3 systems.}
  1011.  
  1012. For example, you could create a text file named @file{hello}, containing
  1013. the following lines:
  1014.  
  1015. @example
  1016. @group
  1017. #! @var{octave-interpreter-name} -qf
  1018. # a sample Octave program
  1019. printf ("Hello, world!\n");
  1020. @end group
  1021. @end example
  1022.  
  1023. @noindent
  1024. (where @var{octave-interpreter-name} should be replaced with the full
  1025. file name for your Octave binary).  After making this file executable
  1026. (with the @code{chmod} command), you can simply type:
  1027.  
  1028. @example
  1029. hello
  1030. @end example
  1031.  
  1032. @noindent
  1033. at the shell, and the system will arrange to run Octave as if you had
  1034. typed:
  1035.  
  1036. @example
  1037. octave hello
  1038. @end example
  1039.  
  1040. The line beginning with @samp{#!} lists the full file name of an
  1041. interpreter to be run, and an optional initial command line argument to
  1042. pass to that interpreter.  The operating system then runs the
  1043. interpreter with the given argument and the full argument list of the
  1044. executed program.  The first argument in the list is the full file name
  1045. of the Octave program. The rest of the argument list will either be
  1046. options to Octave, or data files, or both.  The @samp{-qf} option is
  1047. usually specified in stand-alone Octave programs to prevent them from
  1048. printing the normal startup message, and to keep them from behaving
  1049. differently depending on the contents of a particular user's
  1050. @file{~/.octaverc} file.  @xref{Invoking Octave}.  Note that some
  1051. operating systems may place a limit on the number of characters that are
  1052. recognized after @samp{#!}.
  1053.  
  1054. Self-contained Octave scripts are useful when you want to write a
  1055. program which users can invoke without knowing that the program is
  1056. written in the Octave language.
  1057.  
  1058. If you invoke an executable Octave script with command line arguments,
  1059. the arguments are available in the built-in variable @code{argv}.
  1060. @xref{Command Line Options}.  For example, the following program will
  1061. reproduce the command line that is used to execute it.
  1062.  
  1063. @example
  1064. @group
  1065. #! /bin/octave -qf
  1066. printf ("%s", program_name);
  1067. for i = 1:nargin
  1068.   printf (" %s", argv(i,:));
  1069. endfor
  1070. printf ("\n");
  1071. @end group
  1072. @end example
  1073.  
  1074. @node Comments,  , Executable Octave Programs, Getting Started
  1075. @section Comments in Octave Programs
  1076. @cindex @samp{#}
  1077. @cindex @samp{%}
  1078. @cindex comments
  1079. @cindex use of comments
  1080. @cindex documenting Octave programs
  1081. @cindex programs
  1082.  
  1083. A @dfn{comment} is some text that is included in a program for the sake
  1084. of human readers, and that is not really part of the program.  Comments
  1085. can explain what the program does, and how it works.  Nearly all
  1086. programming languages have provisions for comments, because programs are
  1087. typically hard to understand without them.
  1088.  
  1089. In the Octave language, a comment starts with either the sharp sign
  1090. character, @samp{#}, or the percent symbol @samp{%} and continues to the
  1091. end of the line.  The Octave interpreter ignores the rest of a
  1092. line following a sharp sign or percent symbol.  For example, we could
  1093. have put the following into the function @code{f}:
  1094.  
  1095. @example
  1096. @group
  1097. function xdot = f (x, t)
  1098.  
  1099. # usage: f (x, t)
  1100. #
  1101. # This function defines the right hand
  1102. # side functions for a set of nonlinear
  1103. # differential equations.
  1104.  
  1105.   r = 0.25;
  1106.   @dots{}
  1107. endfunction
  1108. @end group
  1109. @end example
  1110.  
  1111. The @code{help} command (@pxref{Getting Help}) is able to find the first
  1112. block of comments in a function (even those that are composed directly
  1113. on the command line).  This means that users of Octave can use the same
  1114. commands to get help for built-in functions, and for functions that you
  1115. have defined.  For example, after defining the function @code{f} above,
  1116. the command @kbd{help f} produces the output
  1117.  
  1118. @example
  1119. @group
  1120.  usage: f (x, t)
  1121.  
  1122.  This function defines the right hand
  1123.  side functions for a set of nonlinear
  1124.  differential equations.
  1125. @end group
  1126. @end example
  1127.  
  1128. Although it is possible to put comment lines into keyboard-composed
  1129. throw-away Octave programs, it usually isn't very useful, because the
  1130. purpose of a comment is to help you or another person understand the
  1131. program at a later time.
  1132.  
  1133.